<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
       
        tbody{
            background-color: rgb(212, 204, 204);
            td{
                padding: 10px;
               width: 120px;
            }
        }
        thead tr{
            background-color: red;
            text-align: center;
            td{
                padding: 10px;
               width: 120px;
            }
        }
       
    </style>
<!-- </head>全选反选 -->
<body>
    <table border="">
        <thead>
            <tr >
                <td><input type="checkbox" name="" id="evrey"></td>
                <td>商品</td>
                <td>价钱</td>
    
            </tr>
        </thead>
        <tbody id="check">
            <tr>
                <td><input type="checkbox" name="" ></td>
                <td>iphone13</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" ></td>
                <td>iphone13</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" ></td>
                <td>iphone13</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="" ></td>
                <td>iphone13</td>
                <td>8000</td>
            </tr>
        </tbody>
      
    </table>
    <button id="fx" onclick="Fx()">反选</button>
    <script>
        // 获取按钮
        const a=document.getElementById('evrey');
        const b=document.getElementById('check').getElementsByTagName('input');
        // 注册事件
        a.onclick =function(){
            
            console.log(this.checked );
            for(var i=0;i<b.length;i++){
                b[i].checked=this.checked;
            }

        }
        for(var i=0;i<b.length;i++){
            b[i].onclick=function(){
                // 控制全选按钮是否选中
                var c=true;
                // 检测是否全选中
                for(var i=0;i<b.length;i++){
                    if(!b[i].checked){
                        c=false;
                        break;
                    }
                }
                a.checked=c;
            }

        }
        function Fx(){
            for(var i=0;i<b.length;i++){
                if(b[i].checked){
                    b[i].checked=false;
                }else{
                    b[i].checked=true;
                    
                }
            }
           
            
        }
    </script>
</body>
</html>